package com.frxuexi.myroom.repository

import com.frxuexi.lib_base.room.Leave
import com.frxuexi.lib_base.room.LeaveDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

@OptIn
class LeaveRepo @Inject constructor(private  val leaveDao: LeaveDao) {

    fun addLeave(leave: Leave):Flow<Long>{
        return flow { emit(leaveDao.addLeave(leave)) }
    }

    fun selectNoApprove(status:String):Flow<List<Leave>>{
        return flow { emit(leaveDao.selectNoApprove(status)) }
    }

    suspend fun selectLeavesByUid(uid:Int,page:Int,size:Int):List<Leave>{
        return withContext(Dispatchers.IO){
            leaveDao.selectLeavesByUid(uid, page, size)
        }
    }

    suspend fun getCount(uid:Int):Int{
        return withContext(Dispatchers.IO){
            leaveDao.getCount(uid)
        }
    }

    fun update(leave: Leave):Flow<Int>{
        return flow { emit(leaveDao.update(leave)) }
    }
}